package com.gl.rent;

import java.util.List;

import cn.hutool.core.convert.Convert;
import com.gl.common.util.SecurityUtils;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.MetroLine;
import com.haixiaoke.saas.repository.service.IMetroLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 地铁省市-根据id关联地铁号线Controller
 * 
 * @author haixiaoke
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/base/metro")
public class MetroLineController extends BaseController {
    @Autowired
    private IMetroLineService metroService;

    /**
     * 查询地铁省市-根据id关联地铁号线列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MetroLine metro) {
        startPage();
        List<MetroLine> list = metroService.selectMetroList(metro);
        return getDataTable(list);
    }

    /**
     * 获取地铁省市-根据id关联地铁号线详细信息
     */
    @GetMapping(value = "/{metroId}")
    public AjaxResult getInfo(@PathVariable("metroId") Long metroId) {
        return success(metroService.selectMetroByMetroId(metroId));
    }

    /**
     * 新增地铁省市-根据id关联地铁号线
     */
    @PostMapping
    public AjaxResult add(@RequestBody MetroLine metro) {

        //校验线路是否重复
        if (metroService.checkMetroAndCity(metro.getLineName(),metro.getProvinceId(),metro.getCityId())) return error("线路已存在！");

        //填写创建人
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        metro.setCreateBy(id);

        return toAjax(metroService.insertMetro(metro));
    }

    /**
     * 修改地铁省市-根据id关联地铁号线
     */
    @PutMapping
    public AjaxResult edit(@RequestBody MetroLine metroLine) {

        //校验线路是否重复
        if (metroService.checkMetroAndCity(metroLine.getLineName(),metroLine.getProvinceId(),metroLine.getCityId())) return error("线路已存在！");

        //填写修改人
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        metroLine.setUpdateBy(id);

        return toAjax(metroService.updateMetro(metroLine));
    }

    /**
     * 删除地铁省市-根据id关联地铁号线
     */
	@DeleteMapping("/{metroIds}")
    public AjaxResult remove(@PathVariable Long[] metroIds) {
        return toAjax(metroService.deleteMetroByMetroIds(metroIds));
    }

    /**
     * 级联组件数据
     * @param cityId 城市id
     */
    @GetMapping("/selectMetroComboList")
    public AjaxResult selectLinesByCityId(Long cityId) {
        return success(metroService.selectLinesByCityId(cityId));
    }

}
